package cn.pping.system.controller;

import cn.pping.basic.util.JsonResult;
import cn.pping.basic.util.PageList;
import cn.pping.system.domain.DictionaryType;
import cn.pping.system.query.DictionaryTypeQuery;
import cn.pping.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DictionaryTypeController {
    @Autowired
    public IDictionaryTypeService iDictionaryTypeService;

    /**
     * @Title: loadAll 查询全部
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: []
     * @Return java.util.List<cn.pping.org.domain.Departmant>
     */

    @GetMapping
    public List<DictionaryType> loadAll() {
        return iDictionaryTypeService.loadAll();
    }

    /**
     * @Title: loadById 根据ID查询
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 9:37
     * @Parameters: [id]
     * @Return cn.pping.org.domain.Departmant
     */

    @GetMapping("/{id}")
    public DictionaryType loadById(@PathVariable("id") Long id) {
        return iDictionaryTypeService.loadById(id);
    }


    /**
     * @Title: romove 根据Id删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:26
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @DeleteMapping("/{id}")
    public JsonResult romove(@PathVariable("id") Long id) {

        try {
            iDictionaryTypeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }
    @DeleteMapping("/detail/{id}")
    public JsonResult romoveByName(@PathVariable("id") Long id) {

        try {
            iDictionaryTypeService.romoveByName(id);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: batchRomove 批量删除
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/19 19:21
     * @Parameters: [ids]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PatchMapping
    public JsonResult batchRomove(@RequestBody List<Long> ids) {
        try {
            iDictionaryTypeService.batchRomove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    @PatchMapping("/detail")
    public JsonResult batchRemoveByName(@RequestBody List<Long> ids) {
        try {
            iDictionaryTypeService.batchRemoveByName(ids);
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");

        }
    }

    /**
     * @Title: addOrUpdate 添加修改
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:28
     * @Parameters: [departmant]
     * @Return cn.pping.basic.util.JsonResult
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody DictionaryType dictionaryType) {
        try {
            if (dictionaryType.getId() == null) {
                iDictionaryTypeService.add(dictionaryType);
            } else {
                iDictionaryTypeService.update(dictionaryType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * @Title: queryPage 分页
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/17 10:34
     * @Parameters: [id]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query) {
        try {
            return iDictionaryTypeService.pageQuery(query);
        } catch (Exception e) {
            // 应为可能会报错所以前端页面返回一个空的参数 这样前端就显示数值
            return new PageList<>();
        }

    }

}
